package com.wan37.logic.tradingshop.dao.impl;

import com.wan37.logic.tradingshop.dao.TradingShopDao;
import com.wan37.logic.tradingshop.domain.entity.TradingShopDO;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Column;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-08-05 11:16
 **/
@Repository
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class TradingShopDaoImpl implements TradingShopDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;
    @Override
    public boolean add(TradingShopDO tradingShopDO) {
        hibernateTemplate.save(tradingShopDO);
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(TradingShopDO tradingShopDO) {

        return false;
    }

    @Override
    public List<TradingShopDO> getAll() {
        return (List<TradingShopDO>) hibernateTemplate.find("from TradingShopDO ");
    }

    @Override
    public List<TradingShopDO> findByTradingShopId(String tradingShopId) {
        return (List<TradingShopDO>) hibernateTemplate
                .find("from TradingShopDO where tradingShopId = ?", tradingShopId);
    }

    @Override
    public boolean deleteByTradingShopId(String tradingShopId) {
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("delete from TradingShopDO where tradingShopId = ?");
        query.setParameter(0, tradingShopId);
        query.executeUpdate();
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateIngotByTradingShopId(TradingShopDO tradingShopDO) {
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("update TradingShopDO set ingot = ? where tradingShopId = ?");
        query.setParameter(1, tradingShopDO.getTradingShopId());
        query.executeUpdate();
        return false;
    }

}
